/**
 * Copyright (c) 2014-2016 www.nryuncang.com. All Rights Reserved.
 */
package com.topisv.tms.rest.qimen.util.validator;

import java.io.StringWriter;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.xml.bind.JAXB;

import com.topisv.tms.exception.QMErrorCode;
import com.topisv.tms.rest.qimen.qmentity.response.QMReponse;
import com.topisv.tms.rest.qimen.qmentity.response.ResponseFlagType;

/**
 * 校验工具
 *
 * @author yuansheng
 * @version 1.0.0
 */
public class ValidatorUtil {
    
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    
    public static final String SUCCESS_CODE = "success";
    
    public static final String FAILURE_CODE = "failure";
    
    /**
     * 对一个对象进行校验,返回错误信息
     *
     * @param
     * @return
     * @throws
     *             @author
     *             yuansheng
     * @version 1.0.0
     */
    public static <T> String JSRValidator(T obj) {
        if (obj == null) {
            return "object is Null";
        }
        Set<ConstraintViolation<T>> violations = validator.validate(obj);
        if (violations.size() == 0) {
            return SUCCESS_CODE;
        } else {
            StringBuffer buf = new StringBuffer();
            for (ConstraintViolation<T> violation : violations) {
                buf.append(violation.getPropertyPath().toString() + "--");
                buf.append(violation.getMessage() + ";");
            }
            return buf.toString();
        }
    }
    
    /**
     * 对一个对象进行校验,返回奇门错误信息
     *
     * @param
     * @return
     * @throws
     *             @author
     *             yuansheng
     * @version 1.0.0
     */
    public static <T> String qmXmlValidator(T obj) {
        /*String reqValidatorMsg = JSRValidator(obj);
        String result = "";
        if (!SUCCESS_CODE.equals(reqValidatorMsg)) {
            QMReponse qmResponse = new QMReponse();
            qmResponse.setFlag(ResponseFlagType.FAILURE.getType());
            qmResponse.setCode(QMErrorCode.FAILURE.getCode());
            qmResponse.setMessage(reqValidatorMsg);
            StringWriter sw = new StringWriter();
            JAXB.marshal(qmResponse, sw);
            result = sw.toString();
        } else {
            result = SUCCESS_CODE;
        }*/
        
        // return result;
        return SUCCESS_CODE;
        
    }
}
